﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;

namespace NJL.WebManage.FriendLink
{
    public partial class FriendLinkManage : BasePage
    {
        PagedDataSource pds;
        NJL.BLL.NJL_FRIENDLINK bll = new BLL.NJL_FRIENDLINK();
        NJL.Model.NJL_FRIENDLINK model;
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {  
                ViewState["pager"] = "0";
                if (ViewState["edit"] != null)
                {
                    btnUpload.Visible = true;
                }
                if (WEBSITE_CODE == "1")
                {
                    DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlProvince.DataSource = ds;
                    ddlProvince.DataTextField = "AREA_NAME";
                    ddlProvince.DataValueField = "AREA_CODE";
                    ddlProvince.DataBind();
                    ddlarea.DataSource = ds;
                    ddlarea.DataTextField = "AREA_NAME";
                    ddlarea.DataValueField = "AREA_CODE";
                    ddlarea.DataBind();
                }
                else
                {
                    disparea.Visible = false;
                    area.Visible = false;
                }
                bind();
            }
        }
        public void bind()
        {
            DataSet ds = bll.GetList(sql());
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }

            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        protected string sql()
        {
            string sql = "";
            if (WEBSITE_CODE == "1")
            {
                if (ddlarea.SelectedValue == "000000")
                {
                   sql="";
                }
                else 
                {
                    sql+="NJL_SYS_BASE_ID='"+ddlarea.SelectedValue+"'";
                }
            }
            else
            {
                sql = "NJL_SYS_BASE_ID='"+WEBSITE_CODE+"'";
            }
            return sql;

        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {

                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "edit")
            {
                btnUpload.Visible = true;
                btnBack.Visible = true;
                DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                ddlProvince.DataSource = ds;
                ddlProvince.DataTextField = "AREA_NAME";
                ddlProvince.DataValueField = "AREA_CODE";
                ddlProvince.DataBind();              
               
                string id = e.CommandArgument.ToString();
                ViewState["edit"] = id;
                model = bll.GetModel(id);
                if (model != null)
                {
                    ddlProvince.Items.FindByValue(model.NJL_SYS_BASE_ID).Selected = true;
                    txtName.Text = model.FRIENDLINK_NAME;
                    txtSort.Text = model.SORT.ToString();
                    txtUrl.Text = model.FRIENDLINK_URL;

                }

            }
            else if (e.CommandName == "imgbtn1")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_IMAGE_LINK == 1)
                    {
                        model.IS_IMAGE_LINK = 0;
                    }
                    else
                    {
                        model.IS_IMAGE_LINK = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "操作失败");
                }
            }


        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
                ImageButton imgbtn = e.Item.FindControl("imgbtn1") as ImageButton;
                if (imgbtn.AlternateText == "1")
                {
                    imgbtn.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn.AlternateText == "0")
                {
                    imgbtn.ImageUrl = "~/Images/false.gif";
                }
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                string imgurl = GetImageUrl(id);
                Image img = e.Item.FindControl("Image1") as Image;
                ViewState["iamgeid"] = "";
                if (imgurl != "")
                {
                    img.ImageUrl = imgurl;
                    ViewState["iamgeid"] = imgurl;
                }
                else
                {
                    img.Visible = false;
                }
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();

        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();

        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();

        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {

            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();

        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["edit"] == null)
            {  
                model = new Model.NJL_FRIENDLINK();
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.FRIENDLINK_NAME = txtName.Text.Trim();
                model.FRIENDLINK_URL = txtUrl.Text.Trim();
                model.IS_IMAGE_LINK = 0;
                if (WEBSITE_CODE == "1")
                {
                    model.NJL_SYS_BASE_ID = ddlProvince.SelectedValue;
                }
                else
                {
                    model.NJL_SYS_BASE_ID = WEBSITE_CODE;
                }
                model.CREATE_USER_ID = USER_ID;
                model.CREATE_USER_NAME = USER_NAME;
                if (FileUpload1.HasFile)
                {
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                }
                model.CREATE_DATE_TIME = DateTime.Now;
                if (bll.Add(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", Request.RawUrl);
                }

            }
            else
            {
                model = bll.GetModel(ViewState["edit"].ToString());
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.FRIENDLINK_NAME = txtName.Text.Trim();
                model.FRIENDLINK_URL = txtUrl.Text.Trim();
                model.UPDATE_USER_ID = USER_ID;
                model.UPDATE_USER_NAME = USER_NAME;
                if (WEBSITE_CODE == "1")
                {
                    model.NJL_SYS_BASE_ID = ddlProvince.SelectedValue;
                }
                else
                {
                    model.NJL_SYS_BASE_ID = WEBSITE_CODE;
                }
                if (bll.Update(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", Request.RawUrl);
                }
            }

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            model = bll.GetModel(ViewState["edit"].ToString());
            if (FileUpload1.HasFile)
            {
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                        bind();
                    }

                }
            }
        }

        protected void ddlarea_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind();
        }
    }
}